<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\BaseDictionary;
use App\Lib\FileService\FastDFS;

class Activity extends Model
{
    public static $ACTIVITY_TYPE_COMMUNITY = 1;
    public static $ACTIVITY_TYPE_OLDER = 2;
    public static $ACTIVITY_TYPE_GROUP = 3;
    public static $ACTIVITY_TYPE_PARTY = 4;

    public static $ACTIVITY_TYPE_MAP = [
        ['key' => 1, 'text' => '社区任务'],
        ['key' => 2, 'text' => '老人任务'],
        ['key' => 3, 'text' => '团队任务'],
        ['key' => 4, 'text' => '党建活动']
    ];

    public static $USER_JOIN_PERMIT_TYPE_PUBLIC = 1;
    public static $USER_JOIN_PERMIT_TYPE_LIMIT = 2;

    public static $USER_JOIN_PERMIT_TYPE_MAP = [
        ['key' => 1, 'text' => '开放参与'],
        ['key' => 2, 'text' => '限定团队组织成员参与']
    ];

    public static $AUDIT_STATUS_CANCELLED = 1;
    public static $AUDIT_STATUS_DRAFT = 2;
    public static $AUDIT_STATUS_TOAUDIT = 3;
    public static $AUDIT_STATUS_PERMITTED = 4;
    public static $AUDIT_STATUS_DENIED = 5;

    public static $AUDIT_STATUS_MAP = [
        ['key' => 1, 'text' => '已取消'],
        ['key' => 2, 'text' => '待提交'],
        ['key' => 3, 'text' => '待审核'],
        ['key' => 4, 'text' => '已通过'],
        ['key' => 5, 'text' => '未通过']
    ];

    public static $STATUS_UNSTART = 1;
    public static $STATUS_SIGNUP = 2;
    public static $STATUS_STARTING = 3;
    public static $STATUS_FINISHED = 4;

    public static $STATUS_MAP = [
        ['key' => 1, 'text' => '未开始'],
        ['key' => 2, 'text' => '报名中'],
        ['key' => 3, 'text' => '已开始'],
        ['key' => 4, 'text' => '已结束']
    ];

    public $timestamps = true;

    protected $fillable = ['code', 'subject', 'enter_time_start', 'enter_time_end', 'active_time_start', 'active_time_end',
        'activity_type', 'volunteer_num', 'join_method', 'volunteer_rule', 'description', 'min_volunteer_num', 'time_length', 'picture'
    ];

    protected static function boot()
    {
        Activity::saving(function ($activity) {
            $state = UploadedFileState::where('file_id', $activity['picture'])->first();
            if (isset($state)) {
                $state->state = 1;
                $state->save();
            }
        });
    }


    public function cities()
    {
        return $this->belongsToMany('App\City', 'activity_cities', 'activity_id', 'city_id');
    }

    public function sortedCities()
    {
        return $this->cities()->orderBy('sort_order')->get();
    }

    public function groups()
    {
        return $this->belongsToMany('App\UserGroup', 'activity_groups', 'activity_id', 'group_id');
    }

    public function sortedGroups()
    {
        return $this->groups()->orderBy('sort_order')->get();
    }

    public function majorGroups()
    {
        return $this->belongsToMany('App\UserGroup', 'activity_groups', 'activity_id', 'group_id')
            ->wherePivot('major_flag', BaseDictionary::$KEY_YES);
    }

    public function majorGroup()
    {
        return $this->majorGroups()->first();
    }

    public function getFullPicturePath()
    {
        if (!empty($this->picture)) {
            return FastDFS::getFullUrl($this->picture);
        }
        return '/images/no-pic-back.png';
    }

    public function majorGroupIconFull()
    {
        return $this->majorGroup()->getFullIconPath();
    }

    public function getAllCityName()
    {
        $citynames = '';
        foreach ($this->cities as $city) {
            $citynames = $citynames . $city->name . ',';
        }

        if ($citynames !== '') {
            return substr($citynames, 0, strlen($citynames) - 1);
        }

        return '';
    }

    public function getAllGroupsName()
    {
        $groupnames = '';
        foreach ($this->groups as $group) {
            $groupnames = $groupnames . $group->name . ',';
        }

        if ($groupnames !== '') {
            return substr($groupnames, 0, strlen($groupnames) - 1);
        }

        return '';
    }

    public function isPartyActivity()
    {
        return $this->activity_type === self::$ACTIVITY_TYPE_PARTY;
    }


    public function isCommunityActivity()
    {
        return $this->activity_type === self::$ACTIVITY_TYPE_COMMUNITY;
    }

    public function isUnStart()
    {
        return $this->status === self::$STATUS_UNSTART;
    }

    public function isSignUp()
    {
        return $this->status === self::$STATUS_SIGNUP;
    }

    public function isStarting()
    {
        return $this->status === self::$STATUS_STARTING;
    }

    public function isFinished()
    {
        return $this->status === self::$STATUS_FINISHED;
    }
}
